package main.com.liuchong.datadeal;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuchong
 * @create: 2023/3/8 19:51
 * @description:
 */
public class Reconciliation {
    /**
     *
     * @param EIFile 主机文件
     * @param CUFile 银联文件
     * @throws IOException
     */
    public void reconciliation(String EIFile, String CUFile) throws IOException {
        // 银联无，主机有的对账数据
        Map<String, String> EIWithoutCUMap = new LinkedHashMap<>();
        // 银联有，主机无的对账数据
        List<String> CUWithoutEIList = new ArrayList<>();
        // 银联有，主机有，除唯一标识字段外，其它字段信息不一致的对账数据
        List<String> EIWithCUOthersList = new ArrayList<>();
        try {
            //读取文件
            FileInputStream EIFis = new FileInputStream(EIFile);
            BufferedReader EIbr = new BufferedReader(new InputStreamReader(EIFis));
            FileInputStream CUFis = new FileInputStream(CUFile);
            BufferedReader CUbr = new BufferedReader(new InputStreamReader(CUFis));
            String EITemp;
            String CUTemp;
            while ((EITemp = EIbr.readLine()) != null) {
                String[] arrEI = EITemp.split("\\s+");
                EIWithoutCUMap.put(arrEI[0], EITemp);
            }
            while ((CUTemp = CUbr.readLine()) != null) {
                String[] arrCU = CUTemp.split("\\s+");
                // 银联、主机一致数据，移除
                if (EIWithoutCUMap.containsValue(CUTemp)) {
                    EIWithoutCUMap.remove(arrCU[0]);
                } else if (EIWithoutCUMap.containsKey(arrCU[0])) {
                    EIWithCUOthersList.add("CU:" + CUTemp);
                    EIWithCUOthersList.add("EI:" + EIWithoutCUMap.get(arrCU[0]));
                    EIWithCUOthersList.add("");
                } else if (!EIWithoutCUMap.containsKey(arrCU[0])) {
                    CUWithoutEIList.add(CUTemp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map.Entry<String, String>> duplicateList = new ArrayList<>(EIWithoutCUMap.entrySet());
        new BasicFileDeal().writeFile("output/com.liuchong.datadeal.Reconciliation/EIWithoutCUEI.txt", duplicateList);
        writeFileWithList("output/com.liuchong.datadeal.Reconciliation/CUWithoutEI.txt", CUWithoutEIList);
        writeFileWithList("output/com.liuchong.datadeal.Reconciliation/EIWithCUOthers.txt", EIWithCUOthersList);
    }

    private void writeFileWithList (String filePath, List<String> list) throws IOException {
        File file = new File(filePath);
        BufferedWriter bf = new BufferedWriter(new FileWriter(file));
        try {
            for (String s : list) {
                bf.write(s);
                bf.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                bf.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
        Reconciliation reconciliation = new Reconciliation();
        reconciliation.reconciliation("output/EI/Non-duplicate.txt", "output/CU/Non-duplicate.txt");
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");
    }
}
